package com.zx.cloud.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zx.cloud.domain.Resource;
import com.zx.cloud.persistence.CrudMapper;
import com.zx.cloud.persistence.ExtResourceMapper;
import com.zx.cloud.persistence.ResourceMapper;

/**
 * 资源
 */
@Service
public class ResourceService extends CrudServiceImpl<Resource>{
    
    private static final Logger LOGGER = LoggerFactory.getLogger(ResourceService.class);
    
    @Autowired
    private ExtResourceMapper extResourceMapper;
    
    @Autowired
    private ResourceMapper resourceMapper;
    
  	@Autowired
    public ResourceService(CrudMapper<Resource> mapper) {
        super(mapper);
    }

	public List<String> findRoleResource(Long roleId) {
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("roleId", roleId);
		return extResourceMapper.findRoleResource(params);
	}
	
	public List<Resource> findMenusByUserId(Long userId, Long moudleId) {
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("moudleId", moudleId);
		return resourceMapper.findMenusById(params);
	}

	public List<Resource> findPermissionsByUserId(Long userId) {
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("userId", userId);
		return extResourceMapper.findPermissionsByUserId(params);
	}
	
	public int insert(Resource entity,List<Long> roleIds) {
		if(mapper.insert(entity)>0){
			Long resourceId=entity.getId();
			if (null !=roleIds && roleIds.size() > 0) {
				List<Map<Long, Long>> datas=new ArrayList<Map<Long, Long>>();
				for (Long roleId : roleIds) {
					Map<String,Long> params=new HashMap<String, Long>();
					params.put("resourceId", resourceId);
					params.put("roleId", roleId);
				}
				extResourceMapper.insertRoleResource(datas);
			}
		}
		return 1;
	}
	/**
	 * 菜单查询
	 * @param resource
	 * @return
	 */
	public List<Resource>  findAllSource(Resource resource){
		
		List<Resource>  resList = new ArrayList<Resource>();
		//获取全部资源
		List<Resource>  allList =  resourceMapper.findAll();
		if(allList!=null&&allList.size()>0){
			for (Resource firstRes : allList) {
				List<Resource>  secondList = new ArrayList<Resource>();
			    //一级菜单
				if(null==firstRes.getParentId()||0==firstRes.getParentId()){
					for (Resource secondRes : allList) {
						
						List<Resource>  thirdList = new ArrayList<Resource>();
						//二级菜单
						if(secondRes.getParentId()!=null&&(secondRes.getParentId().equals(firstRes.getId()))){
							 //三级级菜单
							 for (Resource thirdRes : allList) {
								  // 查找二级菜单下的子菜单
								  if(thirdRes.getParentId()!=null&& thirdRes.getParentId().equals(secondRes.getId())){
										thirdList.add(thirdRes);
								  }
						      }
							 
							  secondRes.setResources(thirdList);		
							  secondList.add(secondRes);
						 }
					  }
					  firstRes.setResources(secondList);
					  resList.add(firstRes);
			    }
			}
		}
		
		return  resList;
	}
}
